<template>
  <div class="all">
    <div class="top">
      <el-row>
        <el-form ref="form" inline label-position="left">
          <el-col :span="3">
            <el-form-item :label="$t('app.stock.enter.coding')">
              <el-input v-model="query.coding" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item :label="$t('app.stock.enter.name')">
              <el-input v-model="query.name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item :label="$t('app.stock.enter.verifyTime')">
              <el-date-picker v-model="query.submit_date" type="datetimerange" range-separator="-" start-placeholder="" end-placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item :label="$t('app.status')">
              <SelfSelect v-model="query.status" :show-list="enterOption" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item :label="$t('app.stock.enter.source')">
              <SelfSelect v-model="query.type" :show-list="enterType" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="归档">
              <SelfSelect v-model="query.file" :show-list="YN" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item>
              <el-button style="margin-top: 38%" icon="el-icon-search" type="primary" @click="index">查找</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-row class="btns">
        <el-button v-if="roles.includes('StockEnterAdd')" type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd">{{ $t('button.create') }}</el-button>
        <el-button v-if="roles.includes('StockEnterDelete')" type="danger" icon="el-icon-delete" @click="delBtn">{{ $t('button.delete') }}</el-button>
        <el-button v-if="roles.includes('StockEnterAdd')" type="primary" icon="el-icon-download" @click="downloadExcel">下载模板</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="refresh">{{ $t('button.refresh') }}</el-button>
      </el-row>
      <span class="total">{{ $t('app.count') }}：{{ total }}</span>
    </div>
    <div class="main">
      <div class="table">
        <el-table ref="multipleTable" v-loading="loading" border tooltip-effect="dark" width="100%" stripe :data="dataList" @selection-change="handleSelectionChange">
          <el-table-column :label="$t('app.Num')" type="index" align="center" width="50" />
          <el-table-column type="selection" align="center" width="55" />
          <el-table-column prop="coding" :label="$t('app.stock.enter.coding')" align="center" width="180" />
          <el-table-column prop="name" :label="$t('app.stock.enter.name')" align="center" width="180" />
          <el-table-column :label="$t('app.status')" align="center" width="120">
            <template scope="{row}">
              <div v-html="row.status_table" />
            </template>
          </el-table-column>
          <el-table-column prop="verify_time" :label="$t('app.stock.enter.verifyTime')" align="center" width="160" />
          <el-table-column prop="type" :label="$t('app.stock.enter.source')" align="center" />
          <el-table-column prop="store" :label="$t('app.stock.enter.store')" align="center" width="100" />
          <el-table-column prop="file" label="归档" align="center" />
          <el-table-column v-if="roles.includes('StockEnterDetail' || 'StockEnterDel')" :label="$t('app.handle')" fixed="right" width="180" align="center">
            <template scope="{row}">
              <el-button v-if="roles.includes('StockEnterDetail')" type="primary" @click="goDetail(row.id)">{{ $t('button.edit') }}</el-button>
              <el-button v-if="roles.includes('StockEnterDelete')" type="danger" @click="handleDel(row.id)">{{ $t('button.delete') }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination class="page" background :page-size="query.limit" align="center" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
      </div>
    </div>
  </div>
</template>

<script>
import API from '@/api/stock/enter'
import { enterOption, enterType, YN } from '@/utils/option'
import SelfSelect from '@/components/select-o'
import { mapGetters } from 'vuex'
export default {
  components: {
    SelfSelect
  },
  inject: ['goUrl', 'enterStatus', 'reload'],
  data() {
    return {
      data: {},
      dataList: [],
      total: 0,
      loading: true,
      query: {
        limit: 15,
        page: 1,
        kind: 1
      },
      YN,
      disabled: true,
      enterOption,
      enterType,
      multipleSelection: []
    }
  },
  created() {
    const query = JSON.parse(sessionStorage.getItem('query'))
    if (query) {
      this.query = query
      sessionStorage.removeItem('query')
    }
    this.index()
    window.addEventListener('keydown', this.handKeyCode, true)
  },
  computed: {
    ...mapGetters(['roles'])
  },
  methods: {
    index() {
      this.loading = true
      API.enterList(this.query).then(res => {
        this.dataList = res.data.data
        this.total = res.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    setStorage() {
      sessionStorage.setItem('query', JSON.stringify(this.query))
    },
    handKeyCode(e) {
      if(e.keyCode === 13 || e.keyCode === 32){//回车搜索
        this.index()
      }
    },
    goDetail(id) {
      this.setStorage()
      const url = `/stock/enter/edit/${id}`
      this.goUrl(url)
    },
    handleAdd() {
      this.setStorage()
      this.goUrl('/stock/enter/add')
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delete(id).then(res => {
          if (res.data.error_code !== 1) {
            this.$message.error(res.data.message)
          } else {
            this.$message.success('删除成功')
            this.setStorage()
            this.index()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    refresh() {
      this.reload()
    },
    downloadExcel() {
      const a = document.createElement('a')
      a.href = 'https://d.ubeiyun.com/xls/备件入库.xlsx'
      a.download = '模板.xlsx'
      a.style.display = 'none'
      document.body.appendChild(a)
      a.click()
      a.remove()
    },
    delBtn() {
      if (this.multipleSelection && this.multipleSelection.length > 0) {
        const idArray = []
        this.multipleSelection.forEach(item => {
          idArray.push(item.id)
        })
        const id = idArray.join(',')
        this.handleDel(id)
      } else {
        this.$message('请先选择，然后再删除')
      }
    }
  }
}
</script>

<style scoped>

</style>
